package com.caldremch.androidfoundation.base;


import android.os.Bundle;

import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import javax.inject.Inject;

import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.AndroidInjection;

/**
 *
 * @author Caldremch
 * @date 2018/11/5
 * @Email caldremch@163.com
 * @describe 包含网络请求和dagger2
 * @param <T> 具体presenter类型
 **/

public abstract class BasePresenterActivity<T extends  BaseContract.BasePresenter> extends RxAppCompatActivity implements BaseContract.BaseView{

    // TODO: 2018/11/5 AbsActivity 封装

    @Inject
    @Nullable
    protected T mPresenter;

    private Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidInjection.inject(this);
        setContentView(getLayoutId());
        mUnbinder = ButterKnife.bind(this);
        attachView();
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        detachView();
    }

    /**
     * 分离view
     */
    private void detachView() {
        if (mPresenter != null) {
            mPresenter.onDetachView();
        }
    }

    /**
     * 贴上view
     */
    private void attachView() {
        if (mPresenter != null) {
            mPresenter.onAttachView(this);
        }
    }



    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();


}
